package processor;

import entity.Message;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {

    public static final int SERVER_PORT = 3000;

    public ServerSocket serverSocket;

    public void start() throws IOException {
        serverSocket = new ServerSocket(SERVER_PORT);
        while (true) {
            Socket socket = serverSocket.accept();
//            byte[] messageByte = new byte[1024];

            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

            Object obj = null;
            try {
                obj = ois.readObject();
                System.out.println(obj.getClass().getName());

                Message msg = (Message)obj;
                System.out.println(msg.toString());

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (ois != null) {
                        ois.close();
                    }
                }catch (IOException ein) {
                    ein.printStackTrace();
                }

                try {
                    if (oos != null) {
                        oos.close();
                    }
                }catch (IOException ein) {
                    ein.printStackTrace();
                }
            }
        }
    }
}
